package com.yjb.demo.websocket.server.application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

/**
 * @author yjb
 * @description
 * @create 2024/9/23 19:20
 */
@ServerEndpoint(value = "/demo")
@Component
@Slf4j
public class WebSocketServerDemo {

    @OnOpen
    public void onOpen(Session session, EndpointConfig config){
        log.info("open");
        WebSocketConstant.sessionList.add(session);
    }

    @OnClose
    public void onClose(Session session){
        log.info("close");
        WebSocketConstant.sessionList.remove(session);
    }

    @OnMessage
    public void onMessage(String message, Session session){
        log.info(message);
    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("错误,原因: {}", error.getMessage());
    }

}
